package com.gary.springcloud.controller;

import com.gary.springcloud.entity.CommonResult;
import com.gary.springcloud.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author peng_rui
 * @date 2022/4/10
 */
@RestController
public class PaymentController {
    @Resource
    private PaymentService paymentService;

    @GetMapping(value = "/consumer/hystrix/ok")
    public CommonResult<String> hystrixOk() {
        return paymentService.hystrixOk();
    }

    @GetMapping(value = "/consumer/hystrix/timeout")
    @HystrixCommand(fallbackMethod = "hystrixTimeoutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
    })
    public CommonResult<String> hystrixTimeout() {
        return  paymentService.hystrixTimeout();
    }

    public CommonResult<String> hystrixTimeoutHandler() {
        return new CommonResult<>(200, "Order 报告 8001服务器繁忙");
    }
}
